package com.dyrj.question.service.impl;

import com.dyrj.question.bean.PDayanswer;
import com.dyrj.question.bean.PDayaward;
import com.dyrj.question.bean.PDjjrule;
import com.dyrj.question.dao.PDayanswerMapper;
import com.dyrj.question.dao.PDayawardMapper;
import com.dyrj.question.dao.PDjjruleMapper;
import com.dyrj.question.service.IPDjjruleService;
import com.dyrj.utils.DateUtil;
import com.dyrj.utils.StringUtil;
import org.quartz.Scheduler;
import org.springframework.stereotype.Service;
import com.dyrj.base.system.user.model.SysUser;
import com.dyrj.component.properties.UploadProperties;
import com.dyrj.constant.SysFrameConst;
import com.dyrj.utils.UUIDUtil;
import com.dyrj.vo.PageResultMsg;
import com.dyrj.vo.ResultMsg;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.SecurityUtils;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 自动发放代金券规则管理 服务实现类
 * </p>
 *
 * @author admin
 * @since 2021-04-30
 */
@Service
@Log4j2
public class PDjjruleServiceImpl implements IPDjjruleService {

    @Resource
    private PDjjruleMapper mapper;
    @Resource
    private Scheduler scheduler;
    @Resource
    private UploadProperties uploadProperties;
    @Resource
    private PDayanswerMapper pDayanswerMapper;
    @Resource
    private PDayawardMapper pDayawardMapper;
    @Override
    public PageResultMsg table(PDjjrule record) {
        PageInfo<PDjjrule> list = PageHelper.startPage(record.getOffset(), record.getLimit()).doSelectPageInfo(() -> mapper.selectList(record));
        return new PageResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, list.getTotal(), list.getList());
    }


    @Override
    public ResultMsg add(PDjjrule record) {
        String msg = "";
        try {
            mapper.insertSelective(record);
        } catch (Exception e) {
            e.printStackTrace();
            msg = e.getMessage();
        }
        return new ResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, SysFrameConst.ResultCode.SUCCESS.MESSAGE+msg);
    }


    @Override
    public PDjjrule info(String id) {
        PDjjrule ylMmdagl = mapper.selectByPrimaryKey(Integer.parseInt(id));
        return ylMmdagl;
    }

    public ResultMsg update(PDjjrule record) {
        String msg = "";
        try {
            mapper.updateByPrimaryKeySelective(record);
        } catch (Exception e) {
            e.printStackTrace();
            msg = e.getMessage();
        }
        return new ResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, SysFrameConst.ResultCode.SUCCESS.MESSAGE + msg);
    }

    @Override
    public ResultMsg delete(String id) {
        mapper.deleteByPrimaryKey(Integer.parseInt(id));
        return new ResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, SysFrameConst.ResultCode.SUCCESS.MESSAGE);
    }

    public ResultMsg autoSendPrize(String ny){
        String msg=null;
        //获取代金券规则
        List<PDjjrule>  pDjjruleList =  mapper.selectList(null);
        PDayanswer pDayanswer = new PDayanswer();
        pDayanswer.setLimit(1);
        pDayanswer.setOffset(10);
        if("".equals(ny)||ny==null) {
            ny=DateUtil.format(DateUtil.getDate(),"yyyyMM");
        }
        pDayanswer.setNy(ny);
        List<PDayanswer> list = pDayanswerMapper.getPersonRankList(pDayanswer);

        //防止当月重复发放，删除后，再次发放。
        PDayaward selectPDayaward = new PDayaward();
        selectPDayaward.setDtid(Long.valueOf(ny));
        List<PDayaward> list1 =  pDayawardMapper.selectList(selectPDayaward);
        for(int m=0;m<list1.size();m++){
            pDayawardMapper.deleteByPrimaryKey(list1.get(m).getAwardid());
        }

        for(int k=0;k<pDjjruleList.size();k++) {
            PDjjrule pDjjrule = pDjjruleList.get(k);
            String pnum = pDjjrule.getPnum();
            int price = pDjjrule.getPrice();
            String pnumArr[] = StringUtil.split(pnum,"-");
            int qs =0;
            int js =0;
            if(pnumArr.length==2) {
                qs = Integer.parseInt(pnumArr[0]);
                js = Integer.parseInt(pnumArr[1]);
            }else{
                qs=1;
                js=1;
            }
            if(list.size()<=js) {
                js=list.size();
            }
            for (int j = qs; j <= js; j++) {
                PDayanswer pDayanswer1 = list.get(j-1);
                String uid = pDayanswer1.getUid();
                String usrname = pDayanswer1.getUsrname();
                PDayaward pDayaward = new PDayaward();
                pDayaward.setAward(BigDecimal.valueOf(price));
                pDayaward.setUsrname(usrname);
                pDayaward.setUid(uid);
                pDayaward.setAwardtime(DateUtil.getDate());
                pDayaward.setDtid(Long.valueOf(ny));
                pDayaward.setBz("自动发放代金卷");
                int r = pDayawardMapper.insert(pDayaward);
                if (r > 0) {
                    msg="已经插入成功！";
                } else {
                }
            }
        }
        return new ResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, SysFrameConst.ResultCode.SUCCESS.MESSAGE+msg);
    }
}
